import { Injectable } from '@nestjs/common';
import { createTransport, Transporter } from 'nodemailer';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class EmailService {
  transporter: Transporter;
  constructor(private configService: ConfigService) {
    this.transporter = createTransport({
      host: this.configService.get('nodemailer_host'),
      port: this.configService.get('nodemailer_port'),
      secure: false,
      auth: {
        user: this.configService.get('nodemailer_auth_user'),
        pass: this.configService.get('nodemailer_auth_pass'),
      },
      // 163邮箱
      // host: 'smtp.163.com',
      // port: 25,
      // secure: false,
      // auth: {
      //   user: 'tky19989061629@163.com',
      //   pass: 'QYIURGUINGWTYKDT',
      // },
      // qq邮箱
      //     host: 'smtp.qq.com',
      //     port: 587,
      //     secure: false,
      //     auth: {
      //       user: '625151917@qq.com',
      //       pass: 'gevnvqbuymnfbeia',
      //     },
    });
  }

  async sendMail({ to, subject, html }) {
    await this.transporter.sendMail({
      from: {
        name: '会议室预定系统',
        address: this.configService.get('nodemailer_auth_user'),
      },
      to,
      subject,
      html,
    });
  }
}
